#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :拼音注音.py
# @Time      :2024/7/12 
# @Author    :CL
# @email     :1037654919@qq.com
import openpyxl
import pypinyin

def get_pinyin_with_tone_last(chinese_char):
    # 将汉字转换为拼音列表，style 参数设置为 TONE2，表示使用数字表示声调
    pinyin_list = pypinyin.pinyin(chinese_char, heteronym=True, style=pypinyin.Style.TONE3)
    # print(pinyin_list)
    # print(pinyin_list[0])
    #如果 pinyin_list[0][0]的最后一位不是数字，则添加数字5
    for yinpin in pinyin_list[0]:
        if not yinpin[-1].isdigit():
            yinpin += '5'
        yield yinpin



# 测试例子
chinese_char = '与'
for result  in  get_pinyin_with_tone_last(chinese_char):
    print(f"汉字 '{chinese_char}' 的拼音和音调为：{result}")
def main():
    # 打开 Excel 文件
    file_path = '非多音字精标.xlsx'
    workbook = openpyxl.load_workbook(file_path)
    sheet = workbook.active

    # 获取行数
    max_row = sheet.max_row

    # 遍历第一列汉字，将拼音保存到第四列
    for row in range(1, max_row+1):
        chinese_char = sheet.cell(row=row, column=1).value
        if chinese_char:
            # 获取第三列的值
            value_in_col3 = sheet.cell(row=row, column=3).value

            # 获取汉字的拼音和音调
            # pinyin_with_tone = get_pinyin_with_tone_last(chinese_char)
            for i,result in enumerate(get_pinyin_with_tone_last(chinese_char)):
                # 检查第三列的值是否为空，或者与拼音不同
                if value_in_col3 != result:
                    # 将结果写入第四列及以后
                    sheet.cell(row=row, column=4+i, value=result)

    # 保存修改后的 Excel 文件
    workbook.save(file_path)
    print("拼音已成功保存到第四列。")

if __name__ == '__main__':
    print()
    # main()